﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Text;
using System.Reflection;

namespace MShop.Core.Domain
{
    [MetadataType(typeof(Powers_Validation))]
    public partial class Powers
    {
        public RightFlags GetRights(string rightsid)
        {
            RightFlags flags = RightFlags.NoRight;
            if(string.IsNullOrWhiteSpace(rightsid))
            {
                return RightFlags.NoRight;
            }
            else if (!rightsid.Contains(','))
            {
                return (RightFlags)Enum.Parse(typeof(RightFlags), rightsid);
            }
            else
            {
                foreach (string id in rightsid.Split(','))
                {
                    flags = flags | (RightFlags)Enum.Parse(typeof(RightFlags), id);
                }
                bool hasFlag = (flags & RightFlags.NoRight) != 0;
                if (hasFlag)
                {
                    flags = flags & (~RightFlags.NoRight);
                }
                return flags;
            }
             
        }

        public RightFlags GetRights(int rightid)
        {
            RightFlags flags =(RightFlags)Enum.ToObject(typeof(RightFlags), rightid);
            return flags;
        }

        public string RightsString
        {
            get
            {               
                if (Rights != 0)
                {
                    RightFlags flags= GetRights(this.Rights);
                    if (flags == RightFlags.NoRight)
                    {
                        FieldInfo fieldInfo = typeof(RightFlags).GetField("NoRight");
                        RightsDescriptionAttribute attr = fieldInfo.GetCustomAttributes(typeof(RightsDescriptionAttribute), false).OfType<RightsDescriptionAttribute>().FirstOrDefault();
                        return attr.Description;
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        string descAll = null;
                        Enum.GetNames(typeof(RightFlags)).ToList().ForEach((x) =>
                        {
                            RightFlags obj = (RightFlags)Enum.Parse(typeof(RightFlags), x);
                            if ((flags & obj) == obj)
                            {
                              
                                FieldInfo fieldInfo = typeof(RightFlags).GetField(x);
                                RightsDescriptionAttribute attr = fieldInfo.GetCustomAttributes(typeof(RightsDescriptionAttribute), false).OfType<RightsDescriptionAttribute>().FirstOrDefault();
                                if (obj == RightFlags.All)
                                {

                                    descAll= attr.Description;
                                    
                                }
                                else
                                {
                                    sb.Append(attr.Description).Append(", ");
                                }
                            }
                        });
                        if (descAll != null)
                            return descAll;
                        if (sb.Length > 0)
                        {
                            return sb.ToString().Substring(0,sb.Length-2);
                        }
                    }
                  
                    
                }
                return RightFlags.NoRight.ToString();
            }
        }
    }

    [Flags]
    public enum RightFlags
    {
       [RightsDescription("浏览")]
       View=1<<0,
       [RightsDescription("创建")]
       Create=1<<1,
       [RightsDescription("修改")]
       Edit = 1 << 2,
       [RightsDescription("删除")]
       Delete = 1 << 3,
       [RightsDescription("审批")]
       Inspection = 1 << 4,
       [RightsDescription("打印")]
       Print = 1 << 5,
        
       [RightsDescription("所有",Display=false)]
       All=View | Create | Edit | Delete | Inspection |Print,

       
       [RightsDescription("没有权限",Display=false)]
       NoRight=1<<20
    }

    [AttributeUsage(AttributeTargets.Field)]
    public sealed class RightsDescriptionAttribute:Attribute
    {
        public RightsDescriptionAttribute(string description){
             this.Description = description ;
             Display = true;
        }

        public bool Display { get; set; }
        public string  Description{get;set;}
    }

    public partial class Powers_Validation
    {
        [DisplayName("菜单")]        
        public int MenuId { get; set; }
        [DisplayName("角色")]
        public string Role { get; set; }
        [DisplayName("权限")]
        public string Rights { get; set; }
    }
}